import java.io.IOException;

import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import org.elasticsearch.search.SearchHit;

public class Simple {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
	    Node node = null; 
	      node = NodeBuilder.nodeBuilder().node(); 
	      String jsonString1 = "{" + 
	    	        "\"room\":\"livingroom\","+ 
	    	        "\"color\":\"red\" "+ 
	    	        "}"; 
	    	      String jsonString2 = "{" + 
	    	        "\"room\":\"familyroom\","+ 
	    	        "\"color\":\"white\" "+ 
	    	        "}"; 
	    	      String jsonString3 = "{" + 
	    	        "\"room\":\"kitchen\","+ 
	    	        "\"color\":\"blue\" "+ 
	    	        "}"; 

	    	      String jsonString4 = "{" + 
	    	        "\"room\":\"bathroom\","+ 
	    	        "\"color\":\"white\" "+ 
	    	        "}"; 

	    	      String jsonString5 = "{" + 
	    	        "\"room\":\"garage\","+ 
	    	        "\"color\":\"blue\" "+ 
	    	        "}"; 
	      Client client = node.client(); 
	      client.prepareIndex("house", "room", String.valueOf("1")).setSource(jsonString1).execute().actionGet(); 
	      client.prepareIndex("house", "room", String.valueOf("2")).setSource(jsonString2).execute().actionGet(); 
	      client.prepareIndex("house", "room", String.valueOf("3")).setSource(jsonString3).execute().actionGet(); 
	      client.prepareIndex("house", "room", String.valueOf("4")).setSource(jsonString4).execute().actionGet(); 
	      client.prepareIndex("house", "room", String.valueOf("5")).setSource(jsonString5).execute().actionGet(); 
			XContentBuilder content = XContentFactory.jsonBuilder().startObject()  
			        .startObject("page")  
			          .startObject("properties")         
			            .startObject("title")  
			              .field("type", "string")             
			              .field("indexAnalyzer", "ik")  
			              .field("searchAnalyzer", "ik")  
			            .endObject()   
			            .startObject("code")  
			              .field("type", "string")           
			              .field("indexAnalyzer", "ik")  
			              .field("searchAnalyzer", "ik")  
			            .endObject()       
			          .endObject()  
			         .endObject()  
			       .endObject();
			
		  PutMappingRequest mappingRequest = Requests.putMappingRequest("house").type("room").source(content); 
	      client.admin().indices().putMapping(mappingRequest).actionGet();  

	      QueryBuilder queryBuilder = QueryBuilders.termQuery("color", "white"); 
	      SearchRequestBuilder searchRequestBuilder = client.prepareSearch("house"); 
	      searchRequestBuilder.setTypes("room"); 
	      searchRequestBuilder.setSearchType(SearchType.DEFAULT); 
	      searchRequestBuilder.setQuery(queryBuilder); 
	      searchRequestBuilder.setFrom(0).setSize(60).setExplain(true); 
	      SearchResponse resp = searchRequestBuilder.execute().actionGet(); 
	      for (SearchHit hit : resp.getHits()){
	    	  System.out.println("Hit ID: "+hit.getId()); 
	    	  System.out.println("Hit Index: "+hit.getIndex()); 
	    	  System.out.println("Hit Score: "+hit.getScore()); 
	    	  System.out.println("Hit Type: "+hit.getType()); 
	    	  System.out.println("-------------------------"); 
	      }
	      node.close(); 
	    } 

}
